package com.lolaage.tracks;

import android.os.Bundle;

import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;
import com.lolaage.tracks.adapter.MyBMapManager;
import com.lolaage.tracks.util.LocationUtils;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-8-6
 * @see : 地图基类， 1.提供我的位置显示
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class MainMapActivity extends MapActivity
{
    protected static final String TAG = "MainMapActivity";    
    protected MapController mMapController;
    protected MapView mMapView;
    private   MyLocationOverlay mLocationOverlay;
    protected MyBMapManager mMapManager;
    
    @Override
    protected void onCreate(Bundle arg0)
    {
        super.onCreate(arg0);
        mMapManager = MyBMapManager.getInstace(this);        
    }
    
    @Override
    public void setContentView(int layoutResID)
    {
        // 设置模板
        super.setContentView(layoutResID);
        // 初始化地图元素
        initMapViews();
    }
    
    @Override
    protected void onResume()
    {
        try {
            super.initMapActivity(mMapManager.getBMapManager());
        }catch(Exception e)
        {
            // 避免重复初始化
        }
        mMapManager.start();
        super.onResume();
    }

    @Override
    protected void onPause()
    {
        super.onPause();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
    }

    @Override
    protected boolean isRouteDisplayed()
    {
        return false;
    }

    protected void showMyPosition(boolean visilable)
    {
        showMyPosition(visilable, true);
    }
    
    protected void showMyPosition(boolean visilable, boolean isMoveto)
    {
        if(visilable)
        {
            if(mLocationOverlay == null)
            {
                this.mLocationOverlay = new MyLocationOverlay(this, this.mMapView);
                this.mMapView.getOverlays().add(this.mLocationOverlay);
            }
            mLocationOverlay.enableMyLocation();
            if(isMoveto)
            {
                mLocationOverlay.runOnFirstFix(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        moveToMyLocation();
                    }
                });
            }
        }else
        {
            if(mLocationOverlay != null)
            {
                mLocationOverlay.disableMyLocation();
            }
        }
    }
    
    protected void moveToMyLocation()
    {
        if(mLocationOverlay != null && mLocationOverlay.getMyLocation() != null)
        {
            LocationUtils.moveTo(mMapView, mLocationOverlay.getMyLocation(), true, false);
        }else
        {
            
        }
    }
    
    private void initMapViews()
    {
        mMapManager.start();
        super.initMapActivity(mMapManager.getBMapManager());
        this.mMapView = ((MapView) findViewById(R.id.bmapView));
        this.mMapView.setBuiltInZoomControls(true);
        this.mMapController = this.mMapView.getController();
        this.mMapController.setZoom(16);
        this.mMapView.setDrawOverlayWhenZooming(true);
    }
}
